#ifndef PUB_ALIYUN_THREAD_H
#define PUB_ALIYUN_THREAD_H

#include    <QThread>
extern "C" {
    #include "MQTTClient.h"
}
#include "ads1115/ads1115.h"
#include "rs485_dev/rs485_dev.h"
#include <QMetaType>
#include "aliyun_link_mode/dtu_4g.h"

/* 采集数据结构体 */
struct gather_data
{
    float voltage;
    float electricity;
    float humiture[2];    //温湿度
};

class pub_aliyun_thread : public QThread
{
    Q_OBJECT
public:
    explicit pub_aliyun_thread();
    explicit pub_aliyun_thread(MQTTClient *client,dtu_4G *dtu_4g_dev);
    virtual ~pub_aliyun_thread();

private:
    bool data_get = true;
    bool is_pub_date = true;    //是否进行数据的发送
    MQTTClient *clent = nullptr;
    MQTTClient_message msg;
    ads1115 *ads1115_dev = nullptr;
    rs485_dev *humiture_dev = nullptr;
    bool is_get_humiture = true;
    dtu_4G *dtu_4g_dev = nullptr;

    enum
    {
        temp=0,     //温度
        humidity,   //湿度
    };

    struct gather_data data{};
    qint16 cache_data_size_max = 900;   //网关离线时缓存的最大数据个数

    QVector<struct gather_data>cache_vessel;    //数据缓存容器

private:
    float get_voltage();    //获取电压值
    float get_electricity();    //获取电流值
    void cache_data(void);  //当网络断开时进行离线数据缓存

protected:
    void    run() Q_DECL_OVERRIDE;

public:
    void set_get_data_state(bool newState);  //设置客户端与阿里云云平台的连接状态
    void set_is_pub_date(bool state);
    QVector<struct gather_data>* get_cache_vessel(void){return &this->cache_vessel;};

    void setCache_data_size_max(const qint16 &value);

private slots:
    void get_humiture(QByteArray val);   //获取温湿度
    void huimture_dev_send_slots(); //向温湿度设备发送数据槽函数

signals:
    void humiture_dev_send_signal();  //向温湿度设备发送数据信号
    void get_data_ok(struct gather_data val);   //数据采集完成信号

};

#endif // PUB_ALIYUN_THREAD_H
